package com.cw.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.cw.common.constant.Constants;
import com.cw.dto.VYqBrdjxxCondition;
import com.cw.entity.*;
import com.cw.mapper2.MedicalInsuranceLogMapper;
import com.cw.mapper2.YqBeizhuMapper;
import com.cw.service.IPatientBedMiddleService;
import com.cw.service.IYqBeizhuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cw.util.HttpUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ly
 * @since 2021-11-11
 */
@Service
public class YqBeizhuServiceImpl extends ServiceImpl<YqBeizhuMapper, YqBeizhu> implements IYqBeizhuService {
    @Autowired
    private IYqBeizhuService YqBeiZhuBaseMapper;
    
    @Resource
    private MedicalInsuranceLogMapper medicalInsuranceLogMapper;

    @Override
    public List<YqBeizhu> getYQBeiZhuList(YqBeizhu request) {
        LambdaQueryWrapper lw = new LambdaQueryWrapper();
        if (request != null && request.getPreid().compareTo(BigDecimal.ZERO) != 0) {
            lw.eq("preid", request.getPreid());
        }
        if (request != null && !request.getBeizhu().isEmpty()) {
            lw.eq("beizhu", request.getBeizhu());
        }
        if (request != null && !request.getCreateBy().isEmpty()) {
            lw.eq("createBy", request.getCreateBy());
        }
        if (request != null) {
            lw.eq("createTime", request.getCreateTime());
        }
        return list(lw);
    }

    @Override
    public List<YqBeizhu> SelectBeiZhu(YqBeizhu preid) {
        List<YqBeizhu> list = baseMapper.getSelectBeiZhu(preid);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(item -> {
                item.setBeizhu(item.getBeizhu() + "(" + item.getCreateBy() + ")");
            });
        }
        return list;
    }

    @Override
    public int updateYBLX(String name, String preId, Integer type) {
        MedicalInsuranceLog medicalInsuranceLog = new MedicalInsuranceLog();
        medicalInsuranceLog.setPreid(preId);
        medicalInsuranceLog.setName(name);
        String realName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
        realName = realName == null || "".equals(realName) ? "System":realName;
        medicalInsuranceLog.setCreateBy(realName);
        medicalInsuranceLog.setCreateTime(new DateTime());
        medicalInsuranceLog.setMedicalInsuranceFlag(type);
        String yblx = (type == 0 ? "异地医保":"苏州医保");
        yblx = "修改医保类型："+yblx;
        medicalInsuranceLog.setActionLog(yblx);
        int insert = medicalInsuranceLogMapper.insert(medicalInsuranceLog);
        return insert;
    }

}
